home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / snd_dma.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  20KB  |  1,022 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // snd_dma.c -- main control for any streaming sound output device
  21.  
  22. #include "quakedef.h"
  23.  
  24. #ifdef _WIN32
  25. #include "winquake.h"
  26. #endif
  27.  
  28. void S_Play(void);
  29. void S_PlayVol(void);
  30. void S_SoundList(void);
  31. void S_Update_();
  32. void S_StopAllSounds(qboolean clear);
  33. void S_StopAllSoundsC(void);
  34.  
  35. // =======================================================================
  36. // Internal sound data & structures
  37. // =======================================================================
  38.  
  39. channel_t   channels[MAX_CHANNELS];
  40. int            total_channels;
  41.  
  42. int                snd_blocked = 0;
  43. static qboolean    snd_ambient = 1;
  44. qboolean        snd_initialized = false;
  45.  
  46. // pointer should go away
  47. volatile dma_t  *shm = 0;
  48. volatile dma_t sn;
  49.  
  50. vec3_t        listener_origin;
  51. vec3_t        listener_forward;
  52. vec3_t        listener_right;
  53. vec3_t        listener_up;
  54. vec_t        sound_nominal_clip_dist=1000.0;
  55.  
  56. int            soundtime;        // sample PAIRS
  57. int           paintedtime;     // sample PAIRS
  58.  
  59.  
  60. #define    MAX_SFX        512
  61. sfx_t        *known_sfx;        // hunk allocated [MAX_SFX]
  62. int            num_sfx;
  63.  
  64. sfx_t        *ambient_sfx[NUM_AMBIENTS];
  65.  
  66. int         desired_speed = 11025;
  67. int         desired_bits = 16;
  68.  
  69. int sound_started=0;
  70.  
  71. cvar_t bgmvolume = {"bgmvolume", "1", true};
  72. cvar_t volume = {"volume", "0.7", true};
  73.  
  74. cvar_t nosound = {"nosound", "0"};
  75. cvar_t precache = {"precache", "1"};
  76. cvar_t loadas8bit = {"loadas8bit", "0"};
  77. cvar_t bgmbuffer = {"bgmbuffer", "4096"};
  78. cvar_t ambient_level = {"ambient_level", "0.3"};
  79. cvar_t ambient_fade = {"ambient_fade", "100"};
  80. cvar_t snd_noextraupdate = {"snd_noextraupdate", "0"};
  81. cvar_t snd_show = {"snd_show", "0"};
  82. cvar_t _snd_mixahead = {"_snd_mixahead", "0.1", true};
  83.  
  84.  
  85. // ====================================================================
  86. // User-setable variables
  87. // ====================================================================
  88.  
  89.  
  90. //
  91. // Fake dma is a synchronous faking of the DMA progress used for
  92. // isolating performance in the renderer.  The fakedma_updates is
  93. // number of times S_Update() is called per second.
  94. //
  95.  
  96. qboolean fakedma = false;
  97. int fakedma_updates = 15;
  98.  
  99.  
  100. void S_AmbientOff (void)
  101. {
  102.     snd_ambient = false;
  103. }
  104.  
  105.  
  106. void S_AmbientOn (void)
  107. {
  108.     snd_ambient = true;
  109. }
  110.  
  111.  
  112. void S_SoundInfo_f(void)
  113. {
  114.     if (!sound_started || !shm)
  115.     {
  116.         Con_Printf ("sound system not started\n");
  117.         return;
  118.     }
  119.     
  120.     Con_Printf("%5d stereo\n", shm->channels - 1);
  121.     Con_Printf("%5d samples\n", shm->samples);
  122.     Con_Printf("%5d samplepos\n", shm->samplepos);
  123.     Con_Printf("%5d samplebits\n", shm->samplebits);
  124.     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
  125.     Con_Printf("%5d speed\n", shm->speed);
  126.     Con_Printf("0x%x dma buffer\n", shm->buffer);
  127.     Con_Printf("%5d total_channels\n", total_channels);
  128. }
  129.  
  130.  
  131. /*
  132. ================
  133. S_Startup
  134. ================
  135. */
  136.  
  137. void S_Startup (void)
  138. {
  139.     int        rc;
  140.  
  141.     if (!snd_initialized)
  142.         return;
  143.  
  144.     if (!fakedma)
  145.     {
  146.         rc = SNDDMA_Init();
  147.  
  148.         if (!rc)
  149.         {
  150. #ifndef    _WIN32
  151.             Con_Printf("S_Startup: SNDDMA_Init failed.\n");
  152. #endif
  153.             sound_started = 0;
  154.             return;
  155.         }
  156.     }
  157.  
  158.     sound_started = 1;
  159. }
  160.  
  161.  
  162. /*
  163. ================
  164. S_Init
  165. ================
  166. */
  167. void S_Init (void)
  168. {
  169.  
  170.     Con_Printf("\nSound Initialization\n");
  171.  
  172.     if (COM_CheckParm("-nosound"))
  173.         return;
  174.  
  175.     if (COM_CheckParm("-simsound"))
  176.         fakedma = true;
  177.  
  178.     Cmd_AddCommand("play", S_Play);
  179.     Cmd_AddCommand("playvol", S_PlayVol);
  180.     Cmd_AddCommand("stopsound", S_StopAllSoundsC);
  181.     Cmd_AddCommand("soundlist", S_SoundList);
  182.     Cmd_AddCommand("soundinfo", S_SoundInfo_f);
  183.  
  184.     Cvar_RegisterVariable(&nosound);
  185.     Cvar_RegisterVariable(&volume);
  186.     Cvar_RegisterVariable(&precache);
  187.     Cvar_RegisterVariable(&loadas8bit);
  188.     Cvar_RegisterVariable(&bgmvolume);
  189.     Cvar_RegisterVariable(&bgmbuffer);
  190.     Cvar_RegisterVariable(&ambient_level);
  191.     Cvar_RegisterVariable(&ambient_fade);
  192.     Cvar_RegisterVariable(&snd_noextraupdate);
  193.     Cvar_RegisterVariable(&snd_show);
  194.     Cvar_RegisterVariable(&_snd_mixahead);
  195.  
  196.     if (host_parms.memsize < 0x800000)
  197.     {
  198.         Cvar_Set ("loadas8bit", "1");
  199.         Con_Printf ("loading all sounds as 8bit\n");
  200.     }
  201.  
  202.  
  203.  
  204.     snd_initialized = true;
  205.  
  206.     S_Startup ();
  207.  
  208.     SND_InitScaletable ();
  209.  
  210.     known_sfx = Hunk_AllocName (MAX_SFX*sizeof(sfx_t), "sfx_t");
  211.     num_sfx = 0;
  212.  
  213. // create a piece of DMA memory
  214.  
  215.     if (fakedma)
  216.     {
  217.         shm = (void *) Hunk_AllocName(sizeof(*shm), "shm");
  218.         shm->splitbuffer = 0;
  219.         shm->samplebits = 16;
  220.         shm->speed = 22050;
  221.         shm->channels = 2;
  222.         shm->samples = 32768;
  223.         shm->samplepos = 0;
  224.         shm->soundalive = true;
  225.         shm->gamealive = true;
  226.         shm->submission_chunk = 1;
  227.         shm->buffer = Hunk_AllocName(1<<16, "shmbuf");
  228.     }
  229.  
  230.     Con_Printf ("Sound sampling rate: %i\n", shm->speed);
  231.  
  232.     // provides a tick sound until washed clean
  233.  
  234. //    if (shm->buffer)
  235. //        shm->buffer[4] = shm->buffer[5] = 0x7f;    // force a pop for debugging
  236.  
  237.     ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav");
  238.     ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav");
  239.  
  240.     S_StopAllSounds (true);
  241. }
  242.  
  243.  
  244. // =======================================================================
  245. // Shutdown sound engine
  246. // =======================================================================
  247.  
  248. void S_Shutdown(void)
  249. {
  250.  
  251.     if (!sound_started)
  252.         return;
  253.  
  254.     if (shm)
  255.         shm->gamealive = 0;
  256.  
  257.     shm = 0;
  258.     sound_started = 0;
  259.  
  260.     if (!fakedma)
  261.     {
  262.         SNDDMA_Shutdown();
  263.     }
  264. }
  265.  
  266.  
  267. // =======================================================================
  268. // Load a sound
  269. // =======================================================================
  270.  
  271. /*
  272. ==================
  273. S_FindName
  274.  
  275. ==================
  276. */
  277. sfx_t *S_FindName (char *name)
  278. {
  279.     int        i;
  280.     sfx_t    *sfx;
  281.  
  282.     if (!name)
  283.         Sys_Error ("S_FindName: NULL\n");
  284.  
  285.     if (Q_strlen(name) >= MAX_QPATH)
  286.         Sys_Error ("Sound name too long: %s", name);
  287.  
  288. // see if already loaded
  289.     for (i=0 ; i < num_sfx ; i++)
  290.         if (!Q_strcmp(known_sfx[i].name, name))
  291.         {
  292.             return &known_sfx[i];
  293.         }
  294.  
  295.     if (num_sfx == MAX_SFX)
  296.         Sys_Error ("S_FindName: out of sfx_t");
  297.     
  298.     sfx = &known_sfx[i];
  299.     strcpy (sfx->name, name);
  300.  
  301.     num_sfx++;
  302.     
  303.     return sfx;
  304. }
  305.  
  306.  
  307. /*
  308. ==================
  309. S_TouchSound
  310.  
  311. ==================
  312. */
  313. void S_TouchSound (char *name)
  314. {
  315.     sfx_t    *sfx;
  316.     
  317.     if (!sound_started)
  318.         return;
  319.  
  320.     sfx = S_FindName (name);
  321.     Cache_Check (&sfx->cache);
  322. }
  323.  
  324. /*
  325. ==================
  326. S_PrecacheSound
  327.  
  328. ==================
  329. */
  330. sfx_t *S_PrecacheSound (char *name)
  331. {
  332.     sfx_t    *sfx;
  333.  
  334.     if (!sound_started || nosound.value)
  335.         return NULL;
  336.  
  337.     sfx = S_FindName (name);
  338.     
  339. // cache it in
  340.     if (precache.value)
  341.         S_LoadSound (sfx);
  342.     
  343.     return sfx;
  344. }
  345.  
  346.  
  347. //=============================================================================
  348.  
  349. /*
  350. =================
  351. SND_PickChannel
  352. =================
  353. */
  354. channel_t *SND_PickChannel(int entnum, int entchannel)
  355. {
  356.     int ch_idx;
  357.     int first_to_die;
  358.     int life_left;
  359.  
  360. // Check for replacement sound, or find the best one to replace
  361.     first_to_die = -1;
  362.     life_left = 0x7fffffff;
  363.     for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
  364.     {
  365.         if (entchannel != 0        // channel 0 never overrides
  366.         && channels[ch_idx].entnum == entnum
  367.         && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
  368.         {    // allways override sound from same entity
  369.             first_to_die = ch_idx;
  370.             break;
  371.         }
  372.  
  373.         // don't let monster sounds override player sounds
  374.         if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
  375.             continue;
  376.  
  377.         if (channels[ch_idx].end - paintedtime < life_left)
  378.         {
  379.             life_left = channels[ch_idx].end - paintedtime;
  380.             first_to_die = ch_idx;
  381.         }
  382.    }
  383.  
  384.     if (first_to_die == -1)
  385.         return NULL;
  386.  
  387.     if (channels[first_to_die].sfx)
  388.         channels[first_to_die].sfx = NULL;
  389.  
  390.     return &channels[first_to_die];    
  391. }       
  392.  
  393. /*
  394. =================
  395. SND_Spatialize
  396. =================
  397. */
  398. void SND_Spatialize(channel_t *ch)
  399. {
  400.     vec_t dot;
  401.     vec_t ldist, rdist, dist;
  402.     vec_t lscale, rscale, scale;
  403.     vec3_t source_vec;
  404.     sfx_t *snd;
  405.  
  406. // anything coming from the view entity will allways be full volume
  407.     if (ch->entnum == cl.viewentity)
  408.     {
  409.         ch->leftvol = ch->master_vol;
  410.         ch->rightvol = ch->master_vol;
  411.         return;
  412.     }
  413.  
  414. // calculate stereo seperation and distance attenuation
  415.  
  416.     snd = ch->sfx;
  417.     VectorSubtract(ch->origin, listener_origin, source_vec);
  418.     
  419.     dist = VectorNormalize(source_vec) * ch->dist_mult;
  420.     
  421.     dot = DotProduct(listener_right, source_vec);
  422.  
  423.     if (shm->channels == 1)
  424.     {
  425.         rscale = 1.0;
  426.         lscale = 1.0;
  427.     }
  428.     else
  429.     {
  430.         rscale = 1.0 + dot;
  431.         lscale = 1.0 - dot;
  432.     }
  433.  
  434. // add in distance effect
  435.     scale = (1.0 - dist) * rscale;
  436.     ch->rightvol = (int) (ch->master_vol * scale);
  437.     if (ch->rightvol < 0)
  438.         ch->rightvol = 0;
  439.  
  440.     scale = (1.0 - dist) * lscale;
  441.     ch->leftvol = (int) (ch->master_vol * scale);
  442.     if (ch->leftvol < 0)
  443.         ch->leftvol = 0;
  444. }           
  445.  
  446.  
  447. // =======================================================================
  448. // Start a sound effect
  449. // =======================================================================
  450.  
  451. void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
  452. {
  453.     channel_t *target_chan, *check;
  454.     sfxcache_t    *sc;
  455.     int        vol;
  456.     int        ch_idx;
  457.     int        skip;
  458.  
  459.     if (!sound_started)
  460.         return;
  461.  
  462.     if (!sfx)
  463.         return;
  464.  
  465.     if (nosound.value)
  466.         return;
  467.  
  468.     vol = fvol*255;
  469.  
  470. // pick a channel to play on
  471.     target_chan = SND_PickChannel(entnum, entchannel);
  472.     if (!target_chan)
  473.         return;
  474.         
  475. // spatialize
  476.     memset (target_chan, 0, sizeof(*target_chan));
  477.     VectorCopy(origin, target_chan->origin);
  478.     target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
  479.     target_chan->master_vol = vol;
  480.     target_chan->entnum = entnum;
  481.     target_chan->entchannel = entchannel;
  482.     SND_Spatialize(target_chan);
  483.  
  484.     if (!target_chan->leftvol && !target_chan->rightvol)
  485.         return;        // not audible at all
  486.  
  487. // new channel
  488.     sc = S_LoadSound (sfx);
  489.     if (!sc)
  490.     {
  491.         target_chan->sfx = NULL;
  492.         return;        // couldn't load the sound's data
  493.     }
  494.  
  495.     target_chan->sfx = sfx;
  496.     target_chan->pos = 0.0;
  497.     target_chan->end = paintedtime + sc->length;    
  498.  
  499. // if an identical sound has also been started this frame, offset the pos
  500. // a bit to keep it from just making the first one louder
  501.     check = &channels[NUM_AMBIENTS];
  502.     for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
  503.     {
  504.         if (check == target_chan)
  505.             continue;
  506.         if (check->sfx == sfx && !check->pos)
  507.         {
  508.             skip = rand () % (int)(0.1*shm->speed);
  509.             if (skip >= target_chan->end)
  510.                 skip = target_chan->end - 1;
  511.             target_chan->pos += skip;
  512.             target_chan->end -= skip;
  513.             break;
  514.         }
  515.         
  516.     }
  517. }
  518.  
  519. void S_StopSound(int entnum, int entchannel)
  520. {
  521.     int i;
  522.  
  523.     for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
  524.     {
  525.         if (channels[i].entnum == entnum
  526.             && channels[i].entchannel == entchannel)
  527.         {
  528.             channels[i].end = 0;
  529.             channels[i].sfx = NULL;
  530.             return;
  531.         }
  532.     }
  533. }
  534.  
  535. void S_StopAllSounds(qboolean clear)
  536. {
  537.     int        i;
  538.  
  539.     if (!sound_started)
  540.         return;
  541.  
  542.     total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;    // no statics
  543.  
  544.     for (i=0 ; i<MAX_CHANNELS ; i++)
  545.         if (channels[i].sfx)
  546.             channels[i].sfx = NULL;
  547.  
  548.     Q_memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
  549.  
  550.     if (clear)
  551.         S_ClearBuffer ();
  552. }
  553.  
  554. void S_StopAllSoundsC (void)
  555. {
  556.     S_StopAllSounds (true);
  557. }
  558.  
  559. void S_ClearBuffer (void)
  560. {
  561.     int        clear;
  562.         
  563. #ifdef _WIN32
  564.     if (!sound_started || !shm || (!shm->buffer && !pDSBuf))
  565. #else
  566.     if (!sound_started || !shm || !shm->buffer)
  567. #endif
  568.         return;
  569.  
  570.     if (shm->samplebits == 8)
  571. #ifdef AMIGA
  572.         clear = 0;
  573. #else
  574.         clear = 0x80;
  575. #endif
  576.     else
  577.         clear = 0;
  578.  
  579. #ifdef _WIN32
  580.     if (pDSBuf)
  581.     {
  582.         DWORD    dwSize;
  583.         DWORD    *pData;
  584.         int        reps;
  585.         HRESULT    hresult;
  586.  
  587.         reps = 0;
  588.  
  589.         while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
  590.         {
  591.             if (hresult != DSERR_BUFFERLOST)
  592.             {
  593.                 Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
  594.                 S_Shutdown ();
  595.                 return;
  596.             }
  597.  
  598.             if (++reps > 10000)
  599.             {
  600.                 Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n");
  601.                 S_Shutdown ();
  602.                 return;
  603.             }
  604.         }
  605.  
  606.         Q_memset(pData, clear, shm->samples * shm->samplebits/8);
  607.  
  608.         pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
  609.     
  610.     }
  611.     else
  612. #endif
  613.     {
  614.         Q_memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
  615.     }
  616. }
  617.  
  618.  
  619. /*
  620. =================
  621. S_StaticSound
  622. =================
  623. */
  624. void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
  625. {
  626.     channel_t    *ss;
  627.     sfxcache_t        *sc;
  628.  
  629.     if (!sfx)
  630.         return;
  631.  
  632.     if (total_channels == MAX_CHANNELS)
  633.     {
  634.         Con_Printf ("total_channels == MAX_CHANNELS\n");
  635.         return;
  636.     }
  637.  
  638.     ss = &channels[total_channels];
  639.     total_channels++;
  640.  
  641.     sc = S_LoadSound (sfx);
  642.     if (!sc)
  643.         return;
  644.  
  645.     if (sc->loopstart == -1)
  646.     {
  647.         Con_Printf ("Sound %s not looped\n", sfx->name);
  648.         return;
  649.     }
  650.     
  651.     ss->sfx = sfx;
  652.     VectorCopy (origin, ss->origin);
  653.     ss->master_vol = vol;
  654.     ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
  655.     ss->end = paintedtime + sc->length;    
  656.     
  657.     SND_Spatialize (ss);
  658. }
  659.  
  660.  
  661. //=============================================================================
  662.  
  663. /*
  664. ===================
  665. S_UpdateAmbientSounds
  666. ===================
  667. */
  668. void S_UpdateAmbientSounds (void)
  669. {
  670.     mleaf_t        *l;
  671.     float        vol;
  672.     int            ambient_channel;
  673.     channel_t    *chan;
  674.  
  675.     if (!snd_ambient)
  676.         return;
  677.  
  678. // calc ambient sound levels
  679.     if (!cl.worldmodel)
  680.         return;
  681.  
  682.     l = Mod_PointInLeaf (listener_origin, cl.worldmodel);
  683.     if (!l || !ambient_level.value)
  684.     {
  685.         for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
  686.             channels[ambient_channel].sfx = NULL;
  687.         return;
  688.     }
  689.  
  690.     for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
  691.     {
  692.         chan = &channels[ambient_channel];    
  693.         chan->sfx = ambient_sfx[ambient_channel];
  694.     
  695.         vol = ambient_level.value * l->ambient_sound_level[ambient_channel];
  696.         if (vol < 8)
  697.             vol = 0;
  698.  
  699.     // don't adjust volume too fast
  700.         if (chan->master_vol < vol)
  701.         {
  702.             chan->master_vol += host_frametime * ambient_fade.value;
  703.             if (chan->master_vol > vol)
  704.                 chan->master_vol = vol;
  705.         }
  706.         else if (chan->master_vol > vol)
  707.         {
  708.             chan->master_vol -= host_frametime * ambient_fade.value;
  709.             if (chan->master_vol < vol)
  710.                 chan->master_vol = vol;
  711.         }
  712.         
  713.         chan->leftvol = chan->rightvol = chan->master_vol;
  714.     }
  715. }
  716.  
  717.  
  718. /*
  719. ============
  720. S_Update
  721.  
  722. Called once each time through the main loop
  723. ============
  724. */
  725. void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
  726. {
  727.     int            i, j;
  728.     int            total;
  729.     channel_t    *ch;
  730.     channel_t    *combine;
  731.  
  732.     if (!sound_started || (snd_blocked > 0))
  733.         return;
  734.  
  735.     VectorCopy(origin, listener_origin);
  736.     VectorCopy(forward, listener_forward);
  737.     VectorCopy(right, listener_right);
  738.     VectorCopy(up, listener_up);
  739.     
  740. // update general area ambient sound sources
  741.     S_UpdateAmbientSounds ();
  742.  
  743.     combine = NULL;
  744.  
  745. // update spatialization for static and dynamic sounds    
  746.     ch = channels+NUM_AMBIENTS;
  747.     for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
  748.     {
  749.         if (!ch->sfx)
  750.             continue;
  751.         SND_Spatialize(ch);         // respatialize channel
  752.         if (!ch->leftvol && !ch->rightvol)
  753.             continue;
  754.  
  755.     // try to combine static sounds with a previous channel of the same
  756.     // sound effect so we don't mix five torches every frame
  757.     
  758.         if (i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
  759.         {
  760.         // see if it can just use the last one
  761.             if (combine && combine->sfx == ch->sfx)
  762.             {
  763.                 combine->leftvol += ch->leftvol;
  764.                 combine->rightvol += ch->rightvol;
  765.                 ch->leftvol = ch->rightvol = 0;
  766.                 continue;
  767.             }
  768.         // search for one
  769.             combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
  770.             for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
  771.                 if (combine->sfx == ch->sfx)
  772.                     break;
  773.                     
  774.             if (j == total_channels)
  775.             {
  776.                 combine = NULL;
  777.             }
  778.             else
  779.             {
  780.                 if (combine != ch)
  781.                 {
  782.                     combine->leftvol += ch->leftvol;
  783.                     combine->rightvol += ch->rightvol;
  784.                     ch->leftvol = ch->rightvol = 0;
  785.                 }
  786.                 continue;
  787.             }
  788.         }
  789.         
  790.         
  791.     }
  792.  
  793. //
  794. // debugging output
  795. //
  796.     if (snd_show.value)
  797.     {
  798.         total = 0;
  799.         ch = channels;
  800.         for (i=0 ; i<total_channels; i++, ch++)
  801.             if (ch->sfx && (ch->leftvol || ch->rightvol) )
  802.             {
  803.                 //Con_Printf ("%3i %3i %s\n", ch->leftvol, ch->rightvol, ch->sfx->name);
  804.                 total++;
  805.             }
  806.         
  807.         Con_Printf ("----(%i)----\n", total);
  808.     }
  809.  
  810. // mix some sound
  811.     S_Update_();
  812. }
  813.  
  814. void GetSoundtime(void)
  815. {
  816.     int        samplepos;
  817.     static    int        buffers;
  818.     static    int        oldsamplepos;
  819.     int        fullsamples;
  820.     
  821.     fullsamples = shm->samples / shm->channels;
  822.  
  823. // it is possible to miscount buffers if it has wrapped twice between
  824. // calls to S_Update.  Oh well.
  825. #ifdef __sun__
  826.     soundtime = SNDDMA_GetSamples();
  827. #else
  828.     samplepos = SNDDMA_GetDMAPos();
  829.  
  830.  
  831.     if (samplepos < oldsamplepos)
  832.     {
  833.         buffers++;                    // buffer wrapped
  834.         
  835.         if (paintedtime > 0x40000000)
  836.         {    // time to chop things off to avoid 32 bit limits
  837.             buffers = 0;
  838.             paintedtime = fullsamples;
  839.             S_StopAllSounds (true);
  840.         }
  841.     }
  842.     oldsamplepos = samplepos;
  843.  
  844.     soundtime = buffers*fullsamples + samplepos/shm->channels;
  845. #endif
  846. }
  847.  
  848. void S_ExtraUpdate (void)
  849. {
  850.  
  851. #ifdef _WIN32
  852.     IN_Accumulate ();
  853. #endif
  854.  
  855.     if (snd_noextraupdate.value)
  856.         return;        // don't pollute timings
  857.     S_Update_();
  858. }
  859.  
  860. void S_Update_(void)
  861. {
  862.     unsigned        endtime;
  863.     int                samps;
  864.     
  865.     if (!sound_started || (snd_blocked > 0))
  866.         return;
  867.  
  868. // Updates DMA time
  869.     GetSoundtime();
  870.  
  871. // check to make sure that we haven't overshot
  872.     if (paintedtime < soundtime)
  873.     {
  874.         //Con_Printf ("S_Update_ : overflow\n");
  875.         paintedtime = soundtime;
  876.     }
  877.  
  878. // mix ahead of current position
  879.     endtime = soundtime + _snd_mixahead.value * shm->speed;
  880.     samps = shm->samples >> (shm->channels-1);
  881.     if (endtime - soundtime > samps)
  882.         endtime = soundtime + samps;
  883.  
  884. #ifdef _WIN32
  885. // if the buffer was lost or stopped, restore it and/or restart it
  886.     {
  887.         DWORD    dwStatus;
  888.  
  889.         if (pDSBuf)
  890.         {
  891.             if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
  892.                 Con_Printf ("Couldn't get sound buffer status\n");
  893.             
  894.             if (dwStatus & DSBSTATUS_BUFFERLOST)
  895.                 pDSBuf->lpVtbl->Restore (pDSBuf);
  896.             
  897.             if (!(dwStatus & DSBSTATUS_PLAYING))
  898.                 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
  899.         }
  900.     }
  901. #endif
  902.  
  903.     S_PaintChannels (endtime);
  904.  
  905.     SNDDMA_Submit ();
  906. }
  907.  
  908. /*
  909. ===============================================================================
  910.  
  911. console functions
  912.  
  913. ===============================================================================
  914. */
  915.  
  916. void S_Play(void)
  917. {
  918.     static int hash=345;
  919.     int     i;
  920.     char name[256];
  921.     sfx_t    *sfx;
  922.     
  923.     i = 1;
  924.     while (i<Cmd_Argc())
  925.     {
  926.         if (!Q_strrchr(Cmd_Argv(i), '.'))
  927.         {
  928.             Q_strcpy(name, Cmd_Argv(i));
  929.             Q_strcat(name, ".wav");
  930.         }
  931.         else
  932.             Q_strcpy(name, Cmd_Argv(i));
  933.         sfx = S_PrecacheSound(name);
  934.         S_StartSound(hash++, 0, sfx, listener_origin, 1.0, 1.0);
  935.         i++;
  936.     }
  937. }
  938.  
  939. void S_PlayVol(void)
  940. {
  941.     static int hash=543;
  942.     int i;
  943.     float vol;
  944.     char name[256];
  945.     sfx_t    *sfx;
  946.     
  947.     i = 1;
  948.     while (i<Cmd_Argc())
  949.     {
  950.         if (!Q_strrchr(Cmd_Argv(i), '.'))
  951.         {
  952.             Q_strcpy(name, Cmd_Argv(i));
  953.             Q_strcat(name, ".wav");
  954.         }
  955.         else
  956.             Q_strcpy(name, Cmd_Argv(i));
  957.         sfx = S_PrecacheSound(name);
  958.         vol = Q_atof(Cmd_Argv(i+1));
  959.         S_StartSound(hash++, 0, sfx, listener_origin, vol, 1.0);
  960.         i+=2;
  961.     }
  962. }
  963.  
  964. void S_SoundList(void)
  965. {
  966.     int        i;
  967.     sfx_t    *sfx;
  968.     sfxcache_t    *sc;
  969.     int        size, total;
  970.  
  971.     total = 0;
  972.     for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
  973.     {
  974.         sc = Cache_Check (&sfx->cache);
  975.         if (!sc)
  976.             continue;
  977.         size = sc->length*sc->width*(sc->stereo+1);
  978.         total += size;
  979.         if (sc->loopstart >= 0)
  980.             Con_Printf ("L");
  981.         else
  982.             Con_Printf (" ");
  983.         Con_Printf("(%2db) %6i : %s\n",sc->width*8,  size, sfx->name);
  984.     }
  985.     Con_Printf ("Total resident: %i\n", total);
  986. }
  987.  
  988.  
  989. void S_LocalSound (char *sound)
  990. {
  991.     sfx_t    *sfx;
  992.  
  993.     if (nosound.value)
  994.         return;
  995.     if (!sound_started)
  996.         return;
  997.         
  998.     sfx = S_PrecacheSound (sound);
  999.     if (!sfx)
  1000.     {
  1001.         Con_Printf ("S_LocalSound: can't cache %s\n", sound);
  1002.         return;
  1003.     }
  1004.     S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
  1005. }
  1006.  
  1007.  
  1008. void S_ClearPrecache (void)
  1009. {
  1010. }
  1011.  
  1012.  
  1013. void S_BeginPrecaching (void)
  1014. {
  1015. }
  1016.  
  1017.  
  1018. void S_EndPrecaching (void)
  1019. {
  1020. }
  1021.  
  1022.